"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】验证操作用户是事件触发器的owner或是超级用户，有权限删除事件触发器
Create At   :   2024/07/11
Owner       :   sungang14
Description :
    预置条件：
        1.创建用户
        CREATE USER u_eventtrigger_0034 sysadmin PASSWORD 'Bigdata@123';
        CREATE USER u_eventtrigger_0034_1 PASSWORD 'Bigdata@123';
        2.切换到用户ltest
        \c - u_eventtrigger_0034
        3.创建事件触发器函数：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0034()
        RETURNS event_trigger
        LANGUAGE plpgsql
        AS $$ BEGIN
        RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
        END;
        $$;
        4、创建事件触发器：
        CREATE EVENT TRIGGER test_eventtri1 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0034();
    测试步骤：
        1、切换普通用户，删除事件触发器
        \c - u_eventtrigger_0034_1
        DROP EVENT TRIGGER test_eventtri1;
        2、切换管理员用户，删除事件触发器
        \c - u_eventtrigger_0034
        DROP EVENT TRIGGER test_eventtri1;
Expect      : 
        1、删除失败，提示owner用户
        2、删除成功
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.comsh.execut_db_sql("drop function if exists u_eventtrigger_0034.f_eventtrigger_0034();")
        self.comsh.execut_db_sql("drop USER if exists u_eventtrigger_0034;")
        self.comsh.execut_db_sql("drop USER if exists u_eventtrigger_0034_1;")
        msg = self.comsh.execut_db_sql("CREATE USER u_eventtrigger_0034  sysadmin PASSWORD 'Bigdata@123';")
        self.logger.info(msg)
        self.assertEqual("CREATE ROLE", msg)
        msg = self.comsh.execut_db_sql("CREATE USER u_eventtrigger_0034_1 PASSWORD 'Bigdata@123';")
        self.logger.info(msg)
        self.assertEqual("CREATE ROLE", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0034() RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;", sql_type=f" -U u_eventtrigger_0034 -W Bigdata@123")
        self.logger.info(msg)
        self.assertEqual("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0034();",
            sql_type=f" -U u_eventtrigger_0034 -W Bigdata@123")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)

    def test_Index_file_damaged(self):
        self.logger.info(
            "u_eventtrigger_0034_1用户删除EVENT TRIGGER:DROP EVENT TRIGGER start_eventtri;")
        msg = self.comsh.execut_db_sql(
            "DROP EVENT TRIGGER start_eventtri;",
            sql_type=f" -U u_eventtrigger_0034_1 -W Bigdata@123")
        self.logger.info(msg)
        self.assertEqual(
            f'ERROR:  must be owner of event trigger start_eventtri\nDETAIL:  N/A', msg)

        self.logger.info(
            "u_eventtrigger_0034用户删除EVENT TRIGGER:DROP EVENT TRIGGER start_eventtri;")
        msg = self.comsh.execut_db_sql(
            "DROP EVENT TRIGGER start_eventtri;",
            sql_type=f" -U u_eventtrigger_0034 -W Bigdata@123")
        self.logger.info(msg)
        self.assertEqual(f'DROP EVENT TRIGGER', msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0034();",
                                       sql_type=f" -U u_eventtrigger_0034 -W Bigdata@123")
        self.logger.info(msg)
        self.assertEqual('DROP FUNCTION', msg)
        # 删除用户
        msg = self.comsh.execut_db_sql("drop USER if exists u_eventtrigger_0034;")
        self.logger.info(msg)
        self.assertEqual('DROP ROLE', msg)
        msg = self.comsh.execut_db_sql("drop USER if exists u_eventtrigger_0034_1;")
        self.logger.info(msg)
        self.assertEqual('DROP ROLE', msg)

        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
